﻿function CalendarInit() {
    "use strict";

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    var strFormatToDate = function(formatStr, dateStr){
        var year = 0;
        var start = -1;
        var len = dateStr.length;
        if((start = formatStr.indexOf('yyyy')) > -1 && start < len){
            year = dateStr.substr(start, 4);
        }
        var month = 0;
        if((start = formatStr.indexOf('MM')) > -1  && start < len){
            month = parseInt(dateStr.substr(start, 2)) - 1;
        }
        var day = 0;
        if((start = formatStr.indexOf('dd')) > -1 && start < len){
            day = parseInt(dateStr.substr(start, 2));
        }
        var hour = 0;
        if( ((start = formatStr.indexOf('HH')) > -1 || (start = formatStr.indexOf('hh')) > 1) && start < len){
            hour = parseInt(dateStr.substr(start, 2));
        }
        var minute = 0;
        if((start = formatStr.indexOf('mm')) > -1  && start < len){
            minute = dateStr.substr(start, 2);
        }
        var second = 0;
        if((start = formatStr.indexOf('ss')) > -1  && start < len){
            second = dateStr.substr(start, 2);
        }
        return new Date(year, month, day, hour, minute, second);
    };
    
    var hdr = {};

    if ($(window).width() <= 767) {
        hdr = { left: 'title', center: '', right: 'prev,today,month,agendaWeek,agendaDay,next' };
    } else {
        hdr = { left: '', center: 'title', right: 'prev,today,month,agendaWeek,agendaDay,next' };
    }

    var initDrag = function (e) {
        // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
        // it doesn't need to have a start or end



        var eventObject = {
            title: $.trim(e.text()), // use the element's text as the event title

            className: $.trim(e.children('span').attr('class')),// use the element's children as the event class
            
            color:$.trim(e.attr('id')),
            id:"add",
            type:$.trim(e.attr('type'))
        };
        // store the Event Object in the DOM element so we can get to it later
        e.data('eventObject', eventObject);

        // make the event draggable using jQuery UI
        e.draggable({
            zIndex: 999,
            revert: true, // will cause the event to go back to its
            revertDuration: 0  //  original position after the drag
        });
    };

    var addEvent = function (title, priority) {
        title = title.length === 0 ? "Untitled Event" : title;

        priority = priority.length === 0 ? "label label-default" : priority;

        var html = $('<li class="external-event"><span class="' + priority + '">' + title + '</span></li>');

        jQuery('#external-events').append(html);
        initDrag(html);
    };
    
    
    

    /* initialize the external events
     -----------------------------------------------------------------*/

    $('#external-events .fc-event').each(function () {
        initDrag($(this));
    });

    $('#add-event').click(function () {
        var title = $('#event-title').val();
        //var priority = $('input:radio[name=priority]:checked').val();
        var priority="testClass";
        addEvent(title, priority);
    });
    /* initialize the calendar
     -----------------------------------------------------------------*/
    
    $('#calendar').fullCalendar({
    	timezone:'local',
        header: hdr,
        /*eventDragStop:function(calEvent, jsEvent,ui,view){
        	var id=calEvent.id;
        	alert(id);
        },*/
        eventDrop:function(calEvent, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view){
        	//var id=calEvent.id;
        	//alert(calEvent.start);
        	//alert(calEvent.end);
        	
        	var startTime=calEvent.start.format("YYYY-MM-DD HH:mm");
        	//alert(startTime);
        	var endTime=calEvent.end.format("YYYY-MM-DD HH:mm");
        	//alert(endTime);
        	//获取当前事件的id:
        	var id=calEvent.id;
        	var uri=window.path+"/mvc/events/modifyDate/"+id+"/"+startTime+"/"+endTime;
        	$.post(uri,function(rs){
        		if(!rs.result){
        			common.showMessage("Modify date failed.");
        		}
        	});
        },
        
        eventMouseover:function( calEvent, jsEvent, view ) {
        
        	//获取当前事件的id:
        	var id=calEvent.id;
        	var uri=window.path+"/mvc/events/data/"+id;
        	
        	//var format=copiedEventObject.start._d.getDate()+"/"+copiedEventObject.start._d.getMonth()+"/"+copiedEventObject.start._d.getFullYear();
            //$("#start").val(format);
        	
        	//var startInfo=calEvent.start.format("YYYY-MM-DD HH:mm");
        	//var endInfo=calEvent.end.format("YYYY-MM-DD HH:mm");
        	//var title=calEvent.title;
        	
        	
        	//alert("startInfo:"+startInfo);
        	//alert("endInfo:"+endInfo);
        	//alert("title:"+title);
        	
        	//$(this).attr("demo-tip-yellow",'123');
        	//$()
        	//alert($(this).attr("title"));
        	//$('#violet').removeAttr("title");
        	//alert(1);
       //$('#violet').attr("title",'Hey, there! This is a tooltip.<br/>abc');
        	//alert(2);
        	
        	/*$.post(uri,function(html){
        		$("#showData").html(html);
        		if(!rs.result){
        			common.showMessage("Retrive event failed.");
        		}
        	});*/
        },
        
        loading:function(isLoading,view){
        	if(!isLoading){
        		//alert("success!");
        		 $('.test').poshytip({
        				className: 'tip-violet',
        				bgImageFrameSize: 9
        			});
        	}
        },
        
       /* eventDragStart:function( event, jsEvent, ui, view ) {
        	alert("type:"+event.type);
        },*/
        
        
        /*buttonText: {
            prev: '<i class="icon-chevron-left"></i>',
            next: '<i class="icon-chevron-right"></i>'
        },*/
        //defaultDate: '2014-11-1',
        editable: true,
        droppable: true, // this allows things to be dropped onto the calendar !!!
        drop: function (date, allDay) { // this function is called when something is dropped
            // retrieve the dropped element's stored Event Object
            var originalEventObject = $(this).data('eventObject');

            // we need to copy it, so that multiple events don't have a reference to the same object
            var copiedEventObject = $.extend({}, originalEventObject);

            // assign it the date that was reported
            copiedEventObject.start = date;
            copiedEventObject.allDay = allDay;
            // render the event on the calendar
            // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
            $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
            var format=copiedEventObject.start._d.getDate()+"/"+copiedEventObject.start._d.getMonth()+"/"+copiedEventObject.start._d.getFullYear()+" "+copiedEventObject.start._d.getHours()+":"+copiedEventObject.start._d.getMinutes();
            //$("#start").val(format);
            $("#myModalLabel").html(copiedEventObject.title+" "+format);
            $("#myModal").modal();
            // is the "remove after drop" checkbox checked?
            if ($('#drop-remove').is(':checked')) {
                // if so, remove the element from the "Draggable Events" list
                //$(this).remove();
            }
        },
        eventClick: function(calEvent, jsEvent, view) {
        	//alert(calEvent.id);
        	//获取当前事件的id:
        	var id=calEvent.id;
        	var uri=window.path+"/mvc/events/data/"+id;
        	$.post(uri,function(html){
        		$("#showData").html(html);
        		/*if(!rs.result){
        			common.showMessage("Retrive event failed.");
        		}*/
        	});
        		
        	
        	
	        /*alert('Event: ' + calEvent.color);
	        alert('Event: ' + calEvent.title);
	        
	        alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
	        alert('View: ' + view.name);*/
	        //根据ID发送异步请求到后台，返回HTML里面的东西
	        //$('#myModal').html(12);
	        $('#updateModal').modal({show:true,backdrop:false})
	        // change the border color just for fun

	    },
        windowResize: function (event, ui) {
            $('#calendar').fullCalendar('render');
        },
        eventSources:[
		              {
		            	  url:path+"/mvc/events/data"
		              }
		              ],
	    eventLimit: true, // for all non-agenda views
		views: {
		          agenda: {
		                      eventLimit: 6 // adjust to 6 only for agendaWeek/agendaDay
		                  }
		        }
        
        
        		
        /*events: [
     			{
     				title: 'All Day Event12',
     				start: '2014-11-1',
     				allDay:true,
     				color:'red',
     				id:1
     			},
     			{
     				title: 'All Day Event13',
     				start: '2014-11-2',
     				color:'yellow',
     				id:2
     			},
     			{
     				title: 'Long Event',
     				start: '2014-11-07',
     				end: '2014-11-10'
     			},
     			{
     				id: 999,
     				title: 'Repeating Event',
     				start: '2014-11-09T16:00:00'
     			},
     			{
     				id: 999,
     				title: 'Repeating Event',
     				start: '2014-11-16T16:00:00'
     			},
     			{
     				title: 'Conference',
     				start: '2014-11-11',
     				end: '2014-11-13'
     			},
     			{
     				title: 'Meeting',
     				start: '2014-11-12T10:30:00',
     				end: '2014-11-12T12:30:00'
     			},
     			{
     				title: 'Lunch',
     				start: '2014-11-12T12:00:00'
     			},
     			{
     				title: 'Meeting',
     				start: '2014-11-12T14:30:00'
     			},
     			{
     				title: 'Happy Hour',
     				start: '2014-11-12T17:30:00'
     			},
     			{
     				title: 'Dinner',
     				start: '2014-11-12T20:00:00'
     			},
     			{
     				title: 'test',
     				start: '2014-11-12T21:00:00'
     			},
     			{
     				title: 'Birthday Party',
     				start: '2014-11-13T07:00:00'
     			},
     			{
     				title: 'Click for Google',
     				url: 'http://google.com/',
     				start: '2014-11-28'
     			}
     			]*/
    });
    
   
}